﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ConeDBServerNS;
using System.IO;
using System.Collections.Generic;

namespace ConeDBServerTest
{
    [TestClass]
    public class ConeDBServerTest
    {
        private ConeDBServer _ConeServer;
        private ConeDatabase _Database;
        private ConeEntity _Entity;
        private const string CreateDBFile = "db.cdb";
        private string DBFileExpectedLocation = ConeProperties.ConeDatabaseLocation + CreateDBFile;

        public ConeDBServerTest()
        {
            
        }

        [TestMethod]
        public void InitServerTest()
        {
            _ConeServer = new ConeDBServer();
           
        }
        [TestMethod]
        public void InitDatabaseTest()
        {
            _Database = new ConeDatabase(ref _ConeServer);
        }
        [TestMethod]
        public void InitConeEntityTest()
        {
            _Entity = new ConeEntity();
        }

        private void SetupServerAndDatabase()
        {
            _ConeServer = new ConeDBServer();
            _Database = new ConeDatabase(ref _ConeServer);
        }
        private void SetupAll()
        {
            SetupServerAndDatabase();
            _Entity = new ConeEntity();
        }

        private void InitializeEntity()
        {
            SetupAll();
            string FileTest;
            _Entity.Tags.Add("file", @"C:\Users\PJ\Desktop\1260489782931.jpg");
            _Entity.Tags.TryGetValue("file", out FileTest);

            using (FileStream fs = new FileStream(FileTest, FileMode.Open))
            {
                _Entity.Data = new byte[(int)fs.Length];
                fs.Read((byte[])_Entity.Data, 0, (int)fs.Length);

            }
            Assert.IsTrue(_Entity.GetBinary().Length > 0);
        }

        [TestMethod]
        public void AccessorDatabaseOwnerTest()
        {
            SetupServerAndDatabase();
            Assert.IsNotNull(_Database.Owner);
        }
        [TestMethod]
        public void AccessorGetNameAndCreateDatabaseError()
        {
            
            SetupServerAndDatabase();
            try
            {
                _Database.CreateDatabase(CreateDBFile);
            }
            catch (IOException)
            {
                Assert.IsTrue(File.Exists(DBFileExpectedLocation), "Database file creation");
                Assert.IsNull(_Database.DatabaseName, "DatabaseName property");
                Assert.IsNull(_Database.FileLocation, "FileLocation property");
            }
        }
        [TestMethod]
        public void AccessorGetNameAndCreateDatabase()
        {
            SetupServerAndDatabase();
            
            File.Delete(DBFileExpectedLocation);
            _Database.CreateDatabase(CreateDBFile);
            Assert.IsTrue(File.Exists(DBFileExpectedLocation), "Database file creation");
            Assert.AreEqual(Path.GetFileNameWithoutExtension(DBFileExpectedLocation), _Database.DatabaseName, "DatabaseName property");
            Assert.AreEqual(DBFileExpectedLocation, _Database.FileLocation, "FileLocation property");
            
        }
        [TestMethod]
        public void AccessorGetNameAndOpenDatabaseError()
        {
           // File.Delete(DBFileExpectedLocation);
            SetupServerAndDatabase();
            try
            {
                _Database.OpenDatabase(CreateDBFile);
            }
            catch (IOException)
            {
                Assert.IsFalse(File.Exists(DBFileExpectedLocation), "Database file accessed");
                Assert.IsNull(_Database.DatabaseName, "DatabaseName property");
                Assert.IsNull(_Database.FileLocation, "FileLocation property");
            }
        }
        [TestMethod]
        public void AccessorGetNameAndOpenDatabase()
        {
            SetupServerAndDatabase();
            _Database.OpenDatabase(CreateDBFile);
            Assert.IsTrue(File.Exists(DBFileExpectedLocation), "Database file acessed");
            Assert.AreEqual(Path.GetFileNameWithoutExtension(DBFileExpectedLocation), _Database.DatabaseName, "DatabaseName property");
            Assert.AreEqual(DBFileExpectedLocation, _Database.FileLocation, "FileLocation property");

        }
        [TestMethod]
        public void CreateEntityObject()
        {
            SetupAll();
            _Entity.Tags.Add("file", @"E:\Pictures\Anime Pictures\1260489782931.jpg");

            string FileTest;
            _Entity.Tags.TryGetValue("File", out FileTest);
            Assert.IsNull(FileTest);

            FileTest = null;
            _Entity.Tags.TryGetValue("file", out FileTest);
            Assert.IsNotNull(FileTest);
        }

        [TestMethod]
        public void AccessorConeEntityGetBinary()
        {
            SetupAll();
            InitializeEntity();
        }
        [TestMethod]
        public void AddEntityToDatabase()
        {
            SetupAll();
            InitializeEntity();
            _Database.OpenDatabase(CreateDBFile);
            ((ICollection<ConeEntity>)_Database).Add(_Entity);
        }
    }
}
